﻿using MagicWorld.DDD.Domain.Types;

namespace MagicWorld.DDD.Domain.Entity
{
    public class Monster : IMovable
    {
        public MonsterId Id { get; private set; }
        public Health Health { get; private set; }
        public MonsterClass MonsterClass { get; private set; }
        public Transform Position { get; private set; } = Transform.ORIGIN;
        public Vector Velocity { get; private set; } = Vector.ZERO;

        public Monster()
        {
        }

        public Monster(MonsterId id, MonsterClass monsterClass)
        {
            Id = id;
            MonsterClass = monsterClass;
            if (id.Value == 0)
            {
                SetDefaultHealth();
            }
        }

        private void SetDefaultHealth()
        {
            switch (MonsterClass)
            {
                case MonsterClass.Orc:
                    Health = new Health(100);
                    break;

                case MonsterClass.Elf:
                    Health = new Health(50);
                    break;

                case MonsterClass.Dragoon:
                    Health = new Health(150);
                    break;

                default:
                    break;
            }
        }

        internal bool IsAlive()
        {
            return Health.Value > 0;
        }

        public Transform GetPosition()
        {
            return Position;
        }

        public Vector GetVelocity()
        {
            return Velocity;
        }

        public void MoveTo(long x, long y)
        {
            Position = new Transform(x, y);
        }

        public void StartMove(long velX, long velY)
        {
            Velocity = new Vector(velX, velY);
        }

        public void StopMove()
        {
            Velocity = Vector.ZERO;
        }

        public void TakeDamage(long damage)
        {
            var hv = Health.Value - damage;
            Health = new Health(hv);
        }

        public bool IsMoving()
        {
            return Velocity.X != 0 || Velocity.Y != 0;
        }
    }

    public class MonsterFactory
    {
        public static Monster Create(MonsterClass monsterClass)
        {
            return new Monster(MonsterId.New, monsterClass);
        }
    }
}